#include "view_time_4.h"
#include "view_time_4.inc"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "view_times.h"
#include "math.h"
#include "temperature/view_temperature.h"
void view_time_show_temperature_float_with_font(int x, int y, float temperature, bool is_centigrade, bool is_center_align, const unsigned char *const *image_tab, int font_interval, int point_interval,
                                                int unit_interval)
{
    Ensures(temperature >= 0);

    // const unsigned char *const *image_tab = num_16x18_TAB;
    int const font_width = image_tab[0][0] + (image_tab[0][1] << 8);
    int const font_height = image_tab[0][2] + (image_tab[0][3] << 8);
    int const interval = font_interval;
    int draw_x = 0;
    if (isnan(temperature))
    {
        if (is_center_align)
        {
            x += 26;
        }
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        return;
    }
    if (!is_centigrade)
    {
        temperature = 32.f + temperature * 1.8f;
    }
    // float interger_part = 0.f;
    // float decimal_part = 0.f;

    // decimal_part = modff(temperature, &interger_part);
    int temperature_int = temperature * 100;
    if (temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
    }
    draw_x = disp_draw_user_image_num_filter_color(x, y, temperature_int / 100, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += point_interval;
    disp_draw_rect_color(draw_x, y + font_height - 3, draw_x + 3 - 1, y + font_height - 1, RGB565_COLOR_WHITE);
    draw_x += 3 + point_interval;
    draw_x = disp_draw_user_image_num_with_digits_filter_color(draw_x, y, temperature_int % 100, 2, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += unit_interval;
    // draw_x += 6;
    if (is_centigrade)
    {
        disp_draw_user_image_filter_color(draw_x, y, icon_centigrade);
    }
    else
    {
        disp_draw_user_image_filter_color(draw_x, y, icon_Fahrenheit);
    }
}

void view_time_4_display(void)
{
    int x = 0;
    int y = 0;
    bool is_connect = disp_ctrl.com_data.i_temp[6];
    int battery_percent = disp_ctrl.com_data.i_temp[7];
    int battery_level = battery_ctrl_get_level(battery_percent);
    bool is_battery_charge = disp_ctrl.com_data.i_temp[8];
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_back_2);

    x = 83;
    y = 10;
    if (is_connect)
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }

    x = 133;
    y = 12;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_battery_back);
    if (is_battery_charge)
    {
        disp_draw_user_image_ignore_color(x + 9, y + 0, RGB565_COLOR_BLACK, bmp_battery_charging);
    }
    else
    { // 2 2 7 2 12 2
        // 4x8 0x9cd3
        uint16_t color = 0x9cd3;
        if (battery_level == 1)
        {
            color = 0x9020;
        }
        x += 2;
        y += 2;
        for (int i = 0; i < battery_level; i++)
        {
            disp_draw_rect_color(x, y, x + 4 - 1, y + 8 - 1, color);
            x += 5;
        }
    }

    int const heart_rate_num_width = 14;
    int const heart_rate_num_height = 16;
    int const heart_rate_num_interval = 1;
    int const heart_rate_start_x = 46;
    int heart_rate_draw_x = heart_rate_start_x;
    int const heart_rate_draw_y = 173;
    if (disp_ctrl.com_data.i_temp[17] < 40 || disp_ctrl.com_data.i_temp[17] > 220)
    {
        disp_draw_rect_color(heart_rate_draw_x, heart_rate_draw_y + heart_rate_num_height / 2 - 3, heart_rate_draw_x + heart_rate_num_width - 1, heart_rate_draw_y + heart_rate_num_height / 2 - 1,
                             RGB565_COLOR_WHITE);
        heart_rate_draw_x += heart_rate_num_width + heart_rate_num_interval;
        disp_draw_rect_color(heart_rate_draw_x, heart_rate_draw_y + heart_rate_num_height / 2 - 3, heart_rate_draw_x + heart_rate_num_width - 1, heart_rate_draw_y + heart_rate_num_height / 2 - 1,
                             RGB565_COLOR_WHITE);
    }
    else
    {
        disp_draw_user_image_num_filter_color(heart_rate_draw_x, heart_rate_draw_y, disp_ctrl.com_data.i_temp[17], heart_rate_num_width + heart_rate_num_interval, num_14x16_TAB);
    }

    disp_draw_user_image_num_with_digits_filter_color(11, 137, disp_ctrl.com_data.i_temp[3], 2, 19, num_17x24_TAB);

    disp_draw_user_image_num_with_digits_filter_color(58, 137, disp_ctrl.com_data.i_temp[4], 2, 19, num_17x24_TAB);

    view_time_show_temperature_float_with_font(127, 141, disp_ctrl.com_data.f_temp[26], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, false, num_14x16_TAB, 0, 1, 1);
    view_time_show_temperature_float_with_font(127, 173, disp_ctrl.com_data.f_temp[25], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, false, num_14x16_TAB, 0, 1, 1);
}
